package me.mingshan.leetcode;

/**
 * https://leetcode.cn/problems/delete-node-in-a-linked-list
 *
 *
 *
 * @author hanjuntao
 * @date 2025/7/9 0009
 */
public class L_237_delete_node_in_a_linked_list {


    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return "val:" + val;
        }
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(4);
        ListNode node2 = new ListNode(5);
        ListNode node3 = new ListNode(1);
        ListNode node4 = new ListNode(9);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        deleteNode(node2);
        System.out.println(node1);
    }

    /**
     * 删除链表中的节点
     *
     * 思路：
     *
     * 1. 将下一个节点的值复制给当前节点
     * 2. 删除下一个节点,当前节点的next指针指向下一个节点的next
     *
     * @param node
     */
    public static void deleteNode(ListNode node) {
        ListNode next = node.next;

        // 将下一个节点的值复制给当前节点
        node.val = next.val;
        // 当前节点的next指针指向下一个节点的next
        node.next = next.next;
        // 删除下一个节点
        next.next = null;
    }


}

